package com.cloud.controller.wxuser;

import com.cloud.commons.utils.message.MessageCode;
import com.cloud.commons.utils.message.MessageDescription;
import com.cloud.commons.utils.message.Result;
import com.cloud.commons.utils.message.ResultUtil;
import com.cloud.model.view.wxuser.WxUserView;
import com.cloud.service.wxuser.WxUserService;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.util.Base64Utils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.UriComponentsBuilder;

/**
 * <b><code>WxUserController</code></b>
 * <p/>
 * WxUser的具体实现的Api类，提供统一的api调用
 * <p/>
 * <b>Creation Time:</b> 2019-10-15 16:15:58.
 *
 * @author ${model.author}
 * @version 1.0.0
 * @since cloud-spring-boot-be 1.0.0
 */
@Api(value = "[Cloud Source]微信用户表接口",tags = "[Cloud Source]微信用户表接口",description = "")
@RestController
@RequestMapping(value = "/api")
public class WxUserController {

    /** The Constant LOG. */
    private static final Logger LOG = LoggerFactory
            .getLogger(WxUserController.class);

    /** The service. */
    @Autowired
    private WxUserService wxUserService;

    @ApiOperation(value = "[Cloud Source]创建微信用户表", notes = "创建一个微信用户表")
    @ApiResponses(value = {
            @ApiResponse(code = 201, message = "successful operation", responseHeaders = @ResponseHeader(name = "location", description = "URL of new created resource", response = String.class) ),
            @ApiResponse(code = 409, message = "conflict"),
            @ApiResponse(code = 500, message = "internal server error") })
    @RequestMapping(value = "/v1/wxusers", method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Result<?> createWxUser(
            @ApiParam(value = "微信用户表", required = true) @RequestBody WxUserView wxUserView) {
        try {
            // 保存实体
            WxUserView wxUserView1 = wxUserService.saveEntity(wxUserView);

            // 封装返回信息
            return ResultUtil.success(MessageCode.SUCCESS_CODE_200, MessageDescription.OPERATION_INSERT_SUCCESS, wxUserView1);
        } catch (Exception t) {
            return ResultUtil.error(MessageCode.ERROR_CODE_500, t.getMessage());
        }
    }

    @ApiOperation(value = "[Cloud Source]删除微信用户表", notes = "通过id删除微信用户表")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "successful request"),
            @ApiResponse(code = 500, message = "internal server error") })
    @RequestMapping(value = "/v1/wxusers/{id}", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Result<?> deleteWxUser(
            @ApiParam(value = "微信用户表id", required = true) @PathVariable(value = "id") Long id) {
        try {
            wxUserService.deleteEntity(id);
            return ResultUtil.success(MessageCode.SUCCESS_CODE_200,MessageDescription.OPERATION_DELETE_SUCCESS,null);
        } catch (Exception t) {
            return ResultUtil.error(MessageCode.ERROR_CODE_500, t.getMessage());
        }
    }

    @ApiOperation(value = "[Cloud Source]删除微信用户表", notes = "批量删除微信用户表")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "successful request"),
            @ApiResponse(code = 500, message = "internal server error") })
    @RequestMapping(value = "/v1/wxusers", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Result<?> deleteWxUsers(
            @ApiParam(value = "微信用户表ids，样例 - 1,2,3", required = true) @RequestParam(value = "condition", required = true) String condition) {
        try {
            wxUserService.deleteEntities(condition);
            // 封装返回信息
            return ResultUtil.success(MessageCode.SUCCESS_CODE_200,MessageDescription.OPERATION_DELETE_SUCCESS,null);
        } catch (Exception t) {
            return ResultUtil.error(MessageCode.ERROR_CODE_500, t.getMessage());
        }
    }

    @ApiOperation(value = "[Cloud Source]更新微信用户表", notes = "更新微信用户表信息")
    @ApiResponses(value = { @ApiResponse(code = 200, message = "successful"),
            @ApiResponse(code = 404, message = "not found"),
            @ApiResponse(code = 409, message = "conflict"),
            @ApiResponse(code = 500, message = "internal Server Error") })
    @RequestMapping(value = "/v1/wxusers/{id}", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Result<?> updateWxUsers(
            @ApiParam(value = "微信用户表id", required = true) @PathVariable(value = "id") Long id,
            @ApiParam(value = "微信用户表信息", required = true) @RequestBody WxUserView wxUserView) {
        try {
            wxUserView.setId(id);
            wxUserService.updateEntity(wxUserView);
            // 封装返回信息
            return ResultUtil.success(MessageCode.SUCCESS_CODE_200,MessageDescription.OPERATION_UPDATE_SUCCESS,wxUserView);
        } catch (Exception t) {
            return ResultUtil.error(MessageCode.ERROR_CODE_500, t.getMessage());
        }
    }

    @ApiOperation(value = "[Cloud Source]获取单个微信用户表", notes = "通过id获取微信用户表")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "successful request"),
            @ApiResponse(code = 500, message = "internal server error") })
    @RequestMapping(value = "/v1/wxusers/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Result<?> getWxUser(
            @ApiParam(value = "微信用户表id", required = true) @PathVariable(value = "id") Long id) {
        try {
            final WxUserView wxUserView = wxUserService.getEntity(id);
            // 封装返回信息
            return ResultUtil.success(MessageCode.SUCCESS_CODE_200,MessageDescription.OPERATION_QUERY_SUCCESS, wxUserView);
        } catch (Exception t) {
            return ResultUtil.error(MessageCode.ERROR_CODE_500, t.getMessage());
        }
    }


    /**
     * 通过code获取用户openId
     * @param code
     * @return
     */
    @ApiOperation(value = "通过code进行登录操作", notes = "通过code进行登录操作")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "successful request"),
            @ApiResponse(code = 500, message = "internal server error")})
    @RequestMapping(value = "/v1/wxusers", method = RequestMethod.GET)
    public Result<?> userLoginByCode(@ApiParam(value = "code", required = true) @RequestParam String code) {
        try {
            WxUserView wxUserView = wxUserService.checkUserLogin(code);
            return ResultUtil.success(MessageCode.SUCCESS_CODE_200,MessageDescription.OPERATION_INSERT_SUCCESS,wxUserView);
        } catch (Exception e) {
            return ResultUtil.error(MessageCode.ERROR_CODE_500,e.getMessage());
        }
    }

}
